Virginia Election Project

Author

Taylor Atkins

This project is similar to the interactive county map work because they both are based around maps using the tidyverse package. To begin we load up the packages that are going to be used as well as the data which was formulated during class.

Comparing Virgnia Gov vs. Prez Races

This project will be using data to compare the most recent gubernatorial and presidential elections in Virginia. To begin we calculate the difference between the gubernatorial results and presidential results for democrat and republican candidates.

Code
joined_vacomparison <- joined_vacomparison %>% 
  mutate(
    dem_dif = round_half_up((pct_mcauliffe - biden_pct), 2),
    rep_dif = round_half_up((pct_youngkin - trump_pct), 2)
  ) 

We then calculate the election results difference and incorporate that with the general election data we began with.

Code
joined_vacomparison <- joined_vacomparison %>% 
  relocate(dem_dif, rep_dif, .after = locality)
head(joined_vacomparison)
# A tibble: 6 × 11
  locality   dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
  <chr>        <dbl>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <int>   <int>   <dbl>
1 ACCOMACK …   -6.31    7.01  7578  9172    44.7    54.1    7878    4948    61.1
2 ALBEMARLE…   -3.63    5.03 42466 20804    65.7    32.2   19141   31919    37.2
3 ALEXANDRI…   -5.08    6.39 66240 14544    80.3    17.6   14013   43866    24.0
4 ALLEGHANY…   -2.37    3.09  2243  5859    27.3    71.4    4530    1518    74.5
5 AMELIA CO…   -5.13    5.9   2411  5390    30.6    68.3    4720    1617    74.2
6 AMHERST C…   -4.92    6.07  5672 11041    33.4    64.9    9731    3897    71  
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
#   ¹​biden_pct, ²​trump_pct, ³​youngkin, ⁴​mcauliffe, ⁵​pct_youngkin
Code
tail(joined_vacomparison)
# A tibble: 6 × 11
  locality   dem_dif rep_dif biden trump biden…¹ trump…² young…³ mcaul…⁴ pct_y…⁵
  <chr>        <dbl>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <int>   <int>   <dbl>
1 WESTMOREL…   -6.32    7.01  4501  5318    45.3    53.5    4614    2971    60.6
2 WILLIAMSB…   -5       6.02  4790  1963    69.6    28.5    1703    3185    34.5
3 WINCHESTE…   -4.06    5.56  6610  5221    54.6    43.1    4137    4294    48.7
4 WISE COUN…   -3.17    3.45  3110 13366    18.7    80.4    9691    1796    83.9
5 WYTHE COU…   -3.18    3.93  3143 11733    20.8    77.8    9458    2043    81.8
6 YORK COUN…   -4.74    6.4  17683 20241    45.6    52.2   17485   12190    58.6
# … with 1 more variable: pct_mcauliffe <dbl>, and abbreviated variable names
#   ¹​biden_pct, ²​trump_pct, ³​youngkin, ⁴​mcauliffe, ⁵​pct_youngkin
Code
#chose variables we want
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)

Above, we select the variables that will be used in the map later. Below, we gather all the data that is needed to produce a map in R.

Code
#pull for VA counties
va_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "VA",
                       output = "wide",
                       geometry = TRUE)

va_counties_withgeo
Code
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       output = "wide",
                       geometry = TRUE)

all_counties_withgeo

This removes the “M” at the end of some variables that were automatically pulled from the census.

Code
#remove MOE columns - they all end with "M"
va_counties_withgeo <- va_counties_withgeo %>%
  select(-ends_with("M"))

va_counties_withgeo

This removes the “E” at the end of some variables that were automatically pulled from the census.

Code
#remove that trailing "E"
colnames(va_counties_withgeo) <- sub("E$", "", colnames(va_counties_withgeo)) # $ means end of string only

va_counties_withgeo

Changes the name of one of the columns of “locality”.

Code
va_counties_withgeo <- va_counties_withgeo %>% 
  mutate(NAM = str_to_upper(NAM))
Code
va_counties_withgeo <- va_counties_withgeo %>%
  rename("locality" = "NAM")

Then, we clean up some of the data on the back-end.

Code
joined_vacomparison <- joined_vacomparison %>% 
  mutate(locality = paste(locality, ", VIRGINIA", sep = ""))
Code
va_counties_data <- full_join(va_counties_withgeo, joined_vacomparison)

Finally we begin to map Virginia and its counties using mapview.

Code
mapview(va_counties_data, zcol = "dem_dif")

Next, we turn off legends, hover text, popups.

Code
mapview(va_counties_data, zcol = "dem_dif", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (116)! 
Interpolating color vector to match number of zcol values.

Here we create and insert custom labels.

Code
mylabel <- glue::glue("{va_counties_data$locality} {va_counties_data$dem_dif}")


mapview(va_counties_data, zcol = "dem_dif", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (116)! 
Interpolating color vector to match number of zcol values.

Next we customize the pop-ups for the counties.

Code
mypopup <- glue::glue("<strong>{va_counties_data$locality}</strong><br />
                      Total Population: {va_counties_data$totalpop}<br />
                      Difference in D Elections: {va_counties_data$dem_dif}<br />
                      Median Income: ${va_counties_data$medincome}<br />
                      Median Age: {va_counties_data$medage}") %>% 
  lapply(htmltools::HTML)

# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
#   lapply(htmltools::HTML)
Code
head(mypopup)
[[1]]
<strong>LEE COUNTY, VIRGINIA</strong><br />
Total Population: 22482<br />
Difference in D Elections: -2.84<br />
Median Income: $37574<br />
Median Age: 45.6

[[2]]
<strong>ROCKBRIDGE COUNTY, VIRGINIA</strong><br />
Total Population: 22663<br />
Difference in D Elections: -2.38<br />
Median Income: $57828<br />
Median Age: 49.2

[[3]]
<strong>ACCOMACK COUNTY, VIRGINIA</strong><br />
Total Population: 33388<br />
Difference in D Elections: -6.31<br />
Median Income: $50601<br />
Median Age: 47.1

[[4]]
<strong>KING GEORGE COUNTY, VIRGINIA</strong><br />
Total Population: 26597<br />
Difference in D Elections: -6.99<br />
Median Income: $101599<br />
Median Age: 38.3

[[5]]
<strong>TAZEWELL COUNTY, VIRGINIA</strong><br />
Total Population: 40615<br />
Difference in D Elections: -2.83<br />
Median Income: $42937<br />
Median Age: 45.1

[[6]]
<strong>BUCKINGHAM COUNTY, VIRGINIA</strong><br />
Total Population: 16832<br />
Difference in D Elections: -6.6<br />
Median Income: $49841<br />
Median Age: 43

Then we incorporate those pop-ups.

Code
mapview(va_counties_data, zcol = "dem_dif", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         popup = mypopup,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (116)! 
Interpolating color vector to match number of zcol values.

This project incorporates work from two separate projects worked on in/for class to create our own new piece. In my case we are able to see the difference in votes for the Democratic party between the 2020 presidential election and the 2022 gubernatorial races while comparing that to the median income and median age of the counties. There is certainly some interesting information that can be pulled from this, but the biggest take away for me was that there isn’t a single counties in Virginia where the democratic governor got a higher percentage of the vote than President Biden did. Perhaps this is saying something about the political leanings of the state or perhaps the election turnout from Democrats for non-presidential races.